package com.lichhao.system.export;

import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.lichhao.system.model.OmAreaBean;

public class OmAreaExcelView extends AbstractExcelView {
	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		List<OmAreaBean> list = (List<OmAreaBean>) model.get("omAreaList");
		
		HSSFSheet sheet = workbook.createSheet("区域列表");
		HSSFRow header = sheet.createRow(0);
		header.createCell(0).setCellValue("区域标识");
		header.createCell(1).setCellValue("区域名称");
		header.createCell(2).setCellValue("区域编码");
		header.createCell(3).setCellValue("上级区域");
		header.createCell(4).setCellValue("区域描述");
		header.createCell(5).setCellValue("创建时间");
		
		int rowNum = 1;
		for (OmAreaBean bean : list) {
			HSSFRow row = sheet.createRow(rowNum++);
			row.createCell(0).setCellValue(bean.getAreaId());
			row.createCell(1).setCellValue(bean.getAreaName());
			row.createCell(2).setCellValue(bean.getAreaCode());
			row.createCell(3).setCellValue(bean.getParntAreaId());
			row.createCell(4).setCellValue(bean.getAreaDesc());
			row.createCell(5).setCellValue(bean.getCreateDate().toString());
		}

//		response.setHeader("Content-Disposition", "inline:filename="
//				+ new String("区域列表".getBytes(), "iso8859-1"));

//		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-disposition", "attachment;filename="
				+ new String("区域列表.xls".getBytes(), "iso8859-1"));
		OutputStream ouputStream = response.getOutputStream();
		workbook.write(ouputStream);
		ouputStream.flush();
		ouputStream.close();

	}
}
